package com;

import com.util.boxs.bodys.BaseBody;
import com.util.boxs.BoxWorld;
import com.util.boxs.HitCallBack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.*;

/**
 * 整个游戏世界
 */
public class World extends JPanel { //2.

    /**
     * 窗口的宽
     */
    public static final int WIDTH = 1300;
    /**
     * 窗口的高
     */
    public static final int HEIGHT = 700;
    /**
     * 运行状态
     */
    public static final int RUNNING = 0;
    /**
     * 游戏结束状态
     */
    public static final int GAME_OVER = 1;
    /**
     * 等待客户端
     */
    private int state = RUNNING; //当前状态(默认为运行状态)

    private boolean choiceHeroBird = false;//鼠标是否选中了小鸟
    private Slingshot slingshot = new Slingshot();//弹弓
    //如下这一堆为窗口中所显示的对象
    private HeroBird heroBird = slingshot.reLoading();
    private Part part = new Part();



    /**
     * 删除越界的对象---避免内存泄漏
     */
    public void outOfBoundsAction() { //每10毫秒走一次
        part.outOfBoundsAction();
    }

    private int score = 0; //玩家的得分


    /**
     * 检测游戏结束
     */
    public void checkGameOverAction() { //每10毫秒走一次

    }

    /**
     * 检测碰撞
     */
    public void bangAction(){
        if(slingshot.isShooting()) {//当小鸟开始飞行时才判断碰撞
            part.bangAction();
        }
    }

    /**
     * 检测死亡
     */
    public void deadAction(){
        part.deadAction();
    }
    /**
     * 启动程序的执行
     */
    public void action() {
        MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("鼠标点击了:"+e.getX()+","+(HEIGHT-e.getY()));
                int x = e.getX();
                int y = HEIGHT-e.getY();//box2d中换算后y从下边界往上算
                BoxWorld.queryBody(new HitCallBack() {
                    @Override
                    public boolean reportBody(BaseBody body) {
                        System.out.println("有");
                        if(heroBird.getBody()==body){
                            System.out.println("是小鸟!");
                            choiceHeroBird = true;
                            return false;
                        }
                        return true;//true则是继续看还有没有被选中的
                    }
                },x,y);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                if(choiceHeroBird){
                    part.resetHit();
                    slingshot.shoot();
                }
                choiceHeroBird = false;
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                if(choiceHeroBird) {
                    slingshot.moveHeroBird(e.getX(), HEIGHT - e.getY());
                }
            }
        };
        this.addMouseListener(ma);
        this.addMouseMotionListener(ma);
        Timer timer = new Timer(); //定时器对象
        int interval = 10; //定时间隔(以毫秒为单位)
        timer.schedule(new TimerTask() {
            public void run() { //定时干的事(每10毫秒自动发生)
                BoxWorld.step();
                deadAction();
                bangAction();
                outOfBoundsAction();    //删除越界的对象
                checkGameOverAction();  //检测游戏结束
                repaint();              //重画(重新调用paint()画)
            }
        }, interval, interval); //定时计划
    }

    /**
     * 重写paint()画  g:画笔
     */
    public void paint(Graphics g) { //每10毫秒走一次
        Graphics2D g2d = (Graphics2D)g;
        switch (state) { //根据当前状态做不同的处理
            case GAME_OVER: //游戏结束状态时，画游戏结束图
//                Images.gameover.paintIcon(null, g, 0, 0);
                break;
            case RUNNING: //运行状态时，画海洋图、对象、分和命
                //绘制背景
                Images.bg.paintIcon(null,g,0,0);

                //绘制弹弓
                slingshot.paintImage(g2d);
                //绘制愤怒小鸟
                heroBird.paintImage(g2d);

                //绘制关卡内容
                part.paintImage(g2d);

                g.drawString("SCORE: " + score, 200, 50); //画分------------不要求掌握
        }
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame(); //3.
        BoxWorld.init(WIDTH,HEIGHT);
        World world = new World();
        world.setFocusable(true);
        frame.setResizable(false);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+5, HEIGHT + 30);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); //1)设置窗口可见  2)尽快调用paint()方法
        world.action();


    }
}

























